using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Models;

namespace DAL
{
	public static partial class HollywoodPicService
	{
        public static bool AddHollywoodPic(HollywoodPic hollywoodPic)
		{
            string sql =
				"INSERT HollywoodPic (Title, BImage, SImage, Location)" +
				"VALUES (@Title, @BImage, @SImage, @Location)";
				
			sql += " ; SELECT @@IDENTITY";

            
			SqlParameter[] para = new SqlParameter[]
			{
				new SqlParameter("@Title", hollywoodPic.Title),
				new SqlParameter("@BImage", hollywoodPic.BImage),
				new SqlParameter("@SImage", hollywoodPic.SImage),
				new SqlParameter("@Location", hollywoodPic.Location)
			};
			
            int i = DBHelper.ExecuteNonQuery(CommandType.Text, sql, para);
			if (i > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
            
		}
		
		
        public static bool DeleteHollywoodPicByID(int iD)
		{
            string sql = "DELETE HollywoodPic WHERE ID = @ID";

           	SqlParameter[] para = new SqlParameter[]
			{
				new SqlParameter("@ID", iD)
			};
		
            int i = DBHelper.ExecuteNonQuery(CommandType.Text, sql, para);
            if (i > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
            
		}
		
        public static bool ModifyHollywoodPic(HollywoodPic hollywoodPic)
        {
            string sql =
                "UPDATE HollywoodPic " +
                "SET " +
	                "Title = @Title, " +
	                "BImage = @BImage, " +
	                "SImage = @SImage, " +
	                "Location = @Location " +
                "WHERE ID = @ID";

			SqlParameter[] para = new SqlParameter[]
			{
				new SqlParameter("@ID", hollywoodPic.ID),
				new SqlParameter("@Title", hollywoodPic.Title),
				new SqlParameter("@BImage", hollywoodPic.BImage),
				new SqlParameter("@SImage", hollywoodPic.SImage),
				new SqlParameter("@Location", hollywoodPic.Location)
			};

			int i = DBHelper.ExecuteNonQuery(CommandType.Text, sql, para);
            if (i > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }		

        public static IList<HollywoodPic> GetAllHollywoodPics()
        {
            string sqlAll = "SELECT * FROM HollywoodPic";
			return GetHollywoodPicsBySql( sqlAll );
        }
		
        public static HollywoodPic GetHollywoodPicByID(int iD)
        {
            string sql = "SELECT * FROM HollywoodPic WHERE ID = @ID";

            SqlParameter[] para = new SqlParameter[]
			{
				new SqlParameter("@ID", iD)
			};
			
            DataTable temp = DBHelper.ExecuteGetDataTable(CommandType.Text, sql, para);
			
			HollywoodPic hollywoodPic = new HollywoodPic();
			
            foreach (DataRow dr in temp.Rows)
            {
				hollywoodPic.ID = (int)dr["ID"];
				hollywoodPic.Title = (string)dr["Title"];
				hollywoodPic.BImage = (string)dr["BImage"];
				hollywoodPic.SImage = (string)dr["SImage"];
				hollywoodPic.Location = (string)dr["Location"];
				
            }
            return hollywoodPic;    
            
        }
		
		
        private static IList<HollywoodPic> GetHollywoodPicsBySql(string safeSql)
        {
            List<HollywoodPic> list = new List<HollywoodPic>();

			using (SqlDataReader dr = DBHelper.ExecuteGetReader(CommandType.Text, safeSql, null))
			{
				while (dr.Read())
				{
					HollywoodPic hollywoodPic = new HollywoodPic();
					
					hollywoodPic.ID = (int)dr["ID"];
					hollywoodPic.Title = (string)dr["Title"];
					hollywoodPic.BImage = (string)dr["BImage"];
					hollywoodPic.SImage = (string)dr["SImage"];
					hollywoodPic.Location = (string)dr["Location"];
	
					list.Add(hollywoodPic);
				}
			}
            return list;
        }
		
        private static IList<HollywoodPic> GetHollywoodPicsBySql(string safeSql, params SqlParameter[] para)
        {
            List<HollywoodPic> list = new List<HollywoodPic>();

			using (SqlDataReader dr = DBHelper.ExecuteGetReader(CommandType.Text, safeSql, para))
			{
				while (dr.Read())
				{
					HollywoodPic hollywoodPic = new HollywoodPic();
					
					hollywoodPic.ID = (int)dr["ID"];
					hollywoodPic.Title = (string)dr["Title"];
					hollywoodPic.BImage = (string)dr["BImage"];
					hollywoodPic.SImage = (string)dr["SImage"];
					hollywoodPic.Location = (string)dr["Location"];
	
					list.Add(hollywoodPic);
				}
			}
            return list;
        }
	}
}
